package com.campus.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.entry.ManagerTeacher;
import com.campus.exception.BusinessException;
import com.campus.pojo.dto.TeacherDto;
import com.campus.response.ResponseResult;
import com.campus.response.ResultCode;
import com.campus.service.ManagerTeacherService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/teacher")
public class ManagerTeacherController {

    @Autowired
    private ManagerTeacherService managerTeacherService;

    // 新增教师
    @PostMapping("/save")
    public ResponseResult<Object> saveTeacher(@RequestBody ManagerTeacher managerTeacher){
        if (Objects.isNull(managerTeacher.getTeacherName())){
            throw new BusinessException(ResultCode.ERROR);
        }
        managerTeacherService.saveTeacher(managerTeacher);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS, null);
    }

    // 根据ID获取教师信息
    @GetMapping("/getById")
    public ResponseResult<ManagerTeacher> getTeacherById(@RequestParam Integer managerTeacherId){
        if (Objects.isNull(managerTeacherId)){
            throw new BusinessException(ResultCode.ERROR);
        }
        ManagerTeacher teacherById = managerTeacherService.getTeacherById(managerTeacherId);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS, teacherById);
    }

    // 根据条件获取教师信息列表
    @GetMapping("/getBy")
    private ResponseResult<List<ManagerTeacher>> getTeacherBy(@RequestBody TeacherDto teacherDto){
        List<ManagerTeacher> list = managerTeacherService.getTeacherBy(teacherDto);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS, list);
    }

    // 更新教师信息
    @PutMapping("/update/{id}")
    public ResponseResult<Object> updateTeacher(@PathVariable Integer id, @RequestBody ManagerTeacher managerTeacher){
        if (Objects.isNull(id)){
            throw new BusinessException(ResultCode.ERROR);
        }
        managerTeacher.setId(id);
        managerTeacherService.updateTeacher(managerTeacher);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS, null);
    }

    // 删除教师
    @DeleteMapping("/delete/{id}")
    public ResponseResult<Object> deleteTeacher(@PathVariable Integer id){
        if (Objects.isNull(id)){
            throw new BusinessException(ResultCode.ERROR);
        }
        managerTeacherService.deleteTeacher(id);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS, null);
    }

    // 分页查询教师信息
    @GetMapping("/list")
    public ResponseResult<Page<ManagerTeacher>> listTeachers(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String teacherName,
            @RequestParam(required = false) String subject,
            @RequestParam(required = false) String school,
            @RequestParam(required = false) String profession) {

        // 设置默认分页参数，如果没有提供 pageNum 和 pageSize
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }

        // 创建一个 TeacherDto 用于封装筛选条件
        TeacherDto teacherDto = new TeacherDto();
        teacherDto.setTeacherName(teacherName);
        teacherDto.setSubject(subject);
        teacherDto.setSchool(school);
        teacherDto.setProfession(profession);

        // 调用 service 层获取分页结果，传入筛选条件
        Page<ManagerTeacher> page = managerTeacherService.listTeachers(pageNum, pageSize, teacherDto);

        // 返回分页结果，封装到 ResponseResult 中
        return new ResponseResult<>(ResultCode.ALL_SUCCESS, page);
    }


    // 获取教师总数
    @GetMapping("/count")
    public ResponseResult<Long> countTeachers() {
        Long count = managerTeacherService.conutTeacher();
        return new ResponseResult<>(ResultCode.ALL_SUCCESS, count);
    }
}
